<?php
/**
 * 测试绑定手机号接口
 * 
 * 使用方法：
 * 1. 确保已安装 EasyWeChat: composer require overtrue/wechat
 * 2. 配置好微信小程序信息
 * 3. 运行测试: php test-bind-phone.php
 */

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/support/bootstrap.php';

use EasyWeChat\Factory;
use app\model\UserModel;

// 测试配置
$testConfig = [
    'app_id' => 'wxd0f2ff6da59cf791',
    'secret' => '1391a775e26613d0b35723e0320e3ff9',
];

echo "=== 微信小程序绑定手机号接口测试 ===\n\n";

// 1. 测试 EasyWeChat 安装
echo "1. 检查 EasyWeChat 安装状态...\n";
try {
    $app = Factory::miniProgram($testConfig);
    echo "✓ EasyWeChat 安装成功\n";
} catch (Exception $e) {
    echo "✗ EasyWeChat 安装失败: " . $e->getMessage() . "\n";
    echo "请运行: composer require overtrue/wechat\n";
    exit(1);
}

// 2. 测试数据库连接
echo "\n2. 检查数据库连接...\n";
try {
    $userCount = UserModel::count();
    echo "✓ 数据库连接成功，用户总数: {$userCount}\n";
} catch (Exception $e) {
    echo "✗ 数据库连接失败: " . $e->getMessage() . "\n";
    exit(1);
}

// 3. 测试用户表结构
echo "\n3. 检查用户表结构...\n";
try {
    $user = new UserModel();
    $table = $user->getTable();
    $columns = \support\Db::select("SHOW COLUMNS FROM {$table}");
    
    $hasPhone = false;
    foreach ($columns as $column) {
        if ($column->Field === 'phone') {
            $hasPhone = true;
            break;
        }
    }
    
    if ($hasPhone) {
        echo "✓ 用户表包含 phone 字段\n";
    } else {
        echo "✗ 用户表缺少 phone 字段\n";
        echo "请运行数据库迁移: php phinx.php migrate\n";
        exit(1);
    }
} catch (Exception $e) {
    echo "✗ 检查表结构失败: " . $e->getMessage() . "\n";
    exit(1);
}

// 4. 测试微信配置
echo "\n4. 检查微信配置...\n";
try {
    $config = config('wechat.mini_app');
    if ($config && isset($config['app_id']) && isset($config['secret'])) {
        echo "✓ 微信配置正确\n";
        echo "  AppID: {$config['app_id']}\n";
        echo "  Secret: " . substr($config['secret'], 0, 8) . "...\n";
    } else {
        echo "✗ 微信配置不完整\n";
        echo "请检查 config/wechat.php 文件\n";
        exit(1);
    }
} catch (Exception $e) {
    echo "✗ 读取微信配置失败: " . $e->getMessage() . "\n";
    exit(1);
}

// 5. 测试控制器方法
echo "\n5. 检查控制器方法...\n";
try {
    $controllerFile = __DIR__ . '/app/v1/controller/UserController.php';
    if (file_exists($controllerFile)) {
        $content = file_get_contents($controllerFile);
        if (strpos($content, 'bindPhone') !== false) {
            echo "✓ 绑定手机号方法存在\n";
        } else {
            echo "✗ 绑定手机号方法不存在\n";
            exit(1);
        }
    } else {
        echo "✗ 控制器文件不存在\n";
        exit(1);
    }
} catch (Exception $e) {
    echo "✗ 检查控制器失败: " . $e->getMessage() . "\n";
    exit(1);
}

echo "\n=== 测试完成 ===\n";
echo "所有检查项目通过！\n";
echo "\n下一步：\n";
echo "1. 启动服务器: php start.php start\n";
echo "2. 在小程序中测试绑定手机号功能\n";
echo "3. 查看接口文档: api-doc/bind-phone-api.md\n";

// 6. 显示测试数据示例
echo "\n=== 测试数据示例 ===\n";
echo "请求地址: POST http://localhost:8787/v1/user/bindPhone\n";
echo "请求头: Authorization: your_token_here\n";
echo "请求体:\n";
echo json_encode([
    'code' => 'test_code_here',
    'encryptedData' => 'test_encrypted_data_here',
    'iv' => 'test_iv_here'
], JSON_PRETTY_PRINT);

echo "\n\n注意：实际使用时需要真实的微信授权数据\n"; 